<?php

namespace app\api\controller\v1\home;


use app\admin\model\DataStoreUser;
use app\admin\model\DataStoreUserType;
use app\api\controller\Api;

class Store extends Api
{

    public function storeType()
    {
        $type = DataStoreUserType::where(['status' => 1])
            ->field('id,name,remarks,icon')
            ->select()->toArray();
        foreach ($type as &$value){
            $value['icon'] =$this->request->domain().$value['icon'];
        }
        $this->success('获取成功', $type);
    }


    /**
     * 获取商铺
     * @return void
     */
    public function get(){

        $store = new DataStoreUser();
        $store = $store->with(['abbreviations','main','decoration'])->where(['status'=>1]);

        if(isset($this->post['id'])){
            $list = $store->where(['id'=>$this->post['id']])->select();
        }else{
            if(isset($this->post['store_type'])){
                $store->where(['store_type'=>$this->post['store_type']]);
            }

            if(isset($this->post['store_name'])){
                $store->whereLike('store_name',$this->post['store_name']);
            }

            if(isset($this->post['lat']) &&isset($this->post['lng'])){
                $distance = 5;
                //1.计算最大最小经纬度范围
                $range  = 180 / pi() * $distance / 6378.138; //搜索 N km 之内
                $lngR   = $range / cos($this->post['lat'] * pi() / 180);
                $maxLat = $this->post['lat'] + $range; //最大纬度
                $minLat = $this->post['lat'] - $range; //最小纬度
                $maxLng = $this->post['lng'] + $lngR; //最大经度
                $minLng = $this->post['lng'] - $lngR; //最小经度
                $store->whereBetween('lat',[$minLat,$maxLat]);
                $store->whereBetween('lat',[$minLng,$maxLng]);
            }
            if(isset($this->post['page'])  && isset($this->post['limit']) ){
                $list =  $store->page($this->post['page'],$this->post['limit']);
            }else{
                $list =$store->page(1,10);
            }
        }

        $this->success('获取成功',$list);

    }

}